<template>
  <div
      class="carousel"
      @mouseenter="stopAutoPlay"
      @mouseleave="startAutoPlay"
  >
    <div
        class="slides"
        :style="slideStyle"
        @mousedown="startDrag"
        @mousemove="onDrag"
        @mouseup="endDrag"
        @mouseleave="endDrag"
        @touchstart="startDrag"
        @touchmove="onDrag"
        @touchend="endDrag"
    >
      <div
          class="slide"
          v-for="(slide, index) in slides"
          :key="index"
      >
        <img :src="slide" alt="">
      </div>
    </div>
    <div class="dots">
      <span
          v-for="(dot, index) in slides.length"
          :key="index"
          :class="{ active: currentIndex === index }"
          @click="goTo(index)"
      ></span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CarouselPage',
  data() {
    return {
      currentIndex: 0,
      timer: null,
      // slides: [
      //   require('@/assets/home/lb1.png'),
      //   require('@/assets/home/lb2.png'),
      //   require('@/assets/home/lb3.png'),
      //   require('@/assets/home/lb4.png'),
      //   require('@/assets/home/lb5.png')
      // ],
      slides: [
        require('@/assets/home/slide/1.jpg'),
        require('@/assets/home/slide/2.jpg'),
        require('@/assets/home/slide/3.jpg'),
        require('@/assets/home/slide/4.jpg'),
        require('@/assets/home/slide/5.jpg')
      ],
      isDragging: false,
      startPosX: 0,
      currentTranslate: 0,
      prevTranslate: 0,
      animationId: null
    }
  },
  computed: {
    slideStyle() {
      return {
        transform: `translateX(calc(-${this.currentIndex * 100}% + ${this.currentTranslate}px)`,
        transition: this.isDragging ? 'none' : 'transform 0.5s ease'
      }
    }
  },
  methods: {
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
      this.resetPosition()
    },
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
      this.resetPosition()
    },
    goTo(index) {
      this.currentIndex = index
      this.resetPosition()
    },
    startAutoPlay() {
      this.timer = setInterval(this.next, 3000)
    },
    stopAutoPlay() {
      clearInterval(this.timer)
    },
    startDrag(e) {
      this.stopAutoPlay()
      this.isDragging = true
      this.startPosX = this.getPositionX(e)
      this.prevTranslate = -this.currentIndex * this.getSlideWidth()

      // 停止任何正在进行的动画
      cancelAnimationFrame(this.animationId)
    },
    onDrag(e) {
      if (!this.isDragging) return
      const currentPosition = this.getPositionX(e)
      this.currentTranslate = currentPosition - this.startPosX
    },
    endDrag() {
      if (!this.isDragging) return
      this.isDragging = false

      const movedBy = this.currentTranslate
      this.currentTranslate = 0

      // 如果拖动距离超过阈值，则切换幻灯片
      if (movedBy < -50) this.next()
      else if (movedBy > 50) this.prev()

      this.startAutoPlay()
    },
    getPositionX(e) {
      return e.type.includes('mouse') ? e.pageX : e.touches[0].clientX
    },
    getSlideWidth() {
      return document.querySelector('.carousel').offsetWidth
    },
    resetPosition() {
      this.currentTranslate = 0
      this.prevTranslate = -this.currentIndex * this.getSlideWidth()
    }
  },
  mounted() {
    this.startAutoPlay()
    // 阻止触摸设备的默认行为，防止页面滚动
    document.querySelector('.slides').addEventListener('touchmove', (e) => {
      if (this.isDragging) e.preventDefault()
    }, { passive: false })
  },
  beforeDestroy() {
    this.stopAutoPlay()
    cancelAnimationFrame(this.animationId)
  }
}
</script>

<style scoped>
.carousel {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 33.25%; /* 16:9比例 (9/16=0.5625) */
  overflow: hidden;
}

.slides {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
}
.slide {
  flex: 0 0 100%;
  height: 100%;
  min-width: 100%;
}
.slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none; /* 防止图片干扰拖动事件 */
}
.dots {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  z-index: 10;
}
.dots span {
  width: 12px;
  height: 12px;
  margin: 0 5px;
  border-radius: 50%;
  background: rgba(255,255,255,0.5);
  cursor: pointer;
  transition: background 0.3s ease;
}
.dots span.active {
  background: white;
}
</style>